package la.ge.common;

import la.common.CommonException;

public class Rect {
	private Point leftBottom;

	private Point rightTop;

	/**
	 * 
	 * @param srcStr
	 *            format:"longitude_west,latitude_south, longitude_east,
	 *            latitude_north"
	 * @return
	 */
	public static Rect genFromStr(String srcStr) throws CommonException {
		if (srcStr == null) {
			throw new CommonException("NULL");
		}
		try {
			double coords[] = new double[4];

			int c = 0;
			String[] coordStrs = srcStr.split( StrPatten.SEPARATOR_SEQ );
			if( coordStrs.length != 4 ) {
				throw new CommonException( "coordStrs.length != 4" );
			}
			for (String coord : coordStrs ) {
				coords[c] = Double.parseDouble(coord);
				c++;
			}
			return new Rect(
					new Point( coords[0], coords[1] ),
					new Point( coords[2], coords[3] )
			);

		} catch (Exception e) {
			throw new CommonException(e);
		}
	}

	public Rect(Point leftBottom, Point rightTop) {
		super();
		this.leftBottom = leftBottom;
		this.rightTop = rightTop;
	}

	public Point getLeftBottom() {
		return leftBottom;
	}

	public Point getRightTop() {
		return rightTop;
	}

	public String toString() {
		final String TAB = ",";

		String retValue = "";

		retValue = "Rect(" + "leftBottom=" + this.leftBottom + TAB
				+ "rightTop=" + this.rightTop + TAB + ")";

		return retValue;
	}

}
